package com.lzm.listendemo.util;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class HttpUtil {

    /*httpUtil*/

    public static void sendRequest(String address, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(address)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendRequestTime(String address, int timeOut, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(timeOut, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(address)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendRequestWithCookie(String address, String session, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(address)
                .header("Cookie", session)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void sendRequestWithCookie(String address, int timeOut, String session, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(timeOut, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(address)
                .header("Cookie", session)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void postRequest(String address, RequestBody body, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .build();
        Request request = new Request.Builder()
                .url(address)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void postRequest(String address, int TimeOut, RequestBody body, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(TimeOut, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(address)
                .post(body)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void postRequest(String address, RequestBody body, String session, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(address)
                .post(body)
                .addHeader("token", session)
                .build();
        client.newCall(request).enqueue(callback);
    }


    public static void postRequestWithCookie(String address, RequestBody body, String session, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(address)
                .post(body)
                .addHeader("Cookie", session)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void postRequest(String address, RequestBody body, String Cookies, String session, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(address)
                .post(body)
                .addHeader("Cookie", Cookies)
                .addHeader("token", session)
                .build();
        client.newCall(request).enqueue(callback);
    }

    public static void postRequestWithCookie(String address, RequestBody body, String Cookie, String token, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(address)
                .post(body)
                .header("Cookie", Cookie)
                .addHeader("token", token)
                .build();
        client.newCall(request).enqueue(callback);
    }


    public static void postRequestWithCookie(String address, int TimeOut, RequestBody body, String session, String token, okhttp3.Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(TimeOut, TimeUnit.SECONDS)
                .build();
        Request request = new Request.Builder()
                .url(address)
                .post(body)
                .header("Cookie", session)
                .addHeader("token", token)
                .build();
        client.newCall(request).enqueue(callback);
    }
}
